package com.shycloud.mido.homework.pattern;

import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.homework.api.vo.HomeworkDashboardReqVO;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class HomeworkDashboardRuleFactory {
	
	@Autowired
    private Map<String, IHomeworkDashboardRule> ruleMap = new ConcurrentHashMap<>();
	
    @PostConstruct
    private void init() {
    	log.debug("iHomeworkDashboardRule load done,size: {}" ,ruleMap.size());
    	for (Map.Entry<String, IHomeworkDashboardRule> entry : ruleMap.entrySet()) {
    		log.debug("rule key ：" + entry.getKey() + ",rule bean：" + entry.getValue());
    	}
    }
    
    public IHomeworkDashboardRule getBeanRule(String rule) {
        return Optional.ofNullable(ruleMap.get(rule)).orElseThrow(() -> new BusinessException("作业数据显示规则未找到"));
    }
    
    public IHomeworkDashboardRule getBeanRule(HomeworkDashboardReqVO vo) {
    	StringJoiner sj = new StringJoiner("_");
    	sj.add(String.valueOf(vo.isStudentFlag())).add(vo.getMode()).add(vo.getIsParentMode()).add(StringUtils.isBlank(vo.getTeacherId()) ?"all" : "one");
    	return this.getBeanRule(sj.toString());
    }
}